package com.platform.api;

import com.alibaba.fastjson.JSONObject;
import com.platform.annotation.IgnoreAuth;
import com.platform.annotation.LoginUser;
import com.platform.entity.ProductVo;
import com.platform.entity.QueryShopProductsInputDto;
import com.platform.entity.ShopVo;
import com.platform.entity.UserVo;
import com.platform.service.ApiProductService;
import com.platform.service.ApiShopService;
import com.platform.util.ApiBaseAction;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhanghao283
 */
@Api(tags = "产品管理")
@RestController
@RequestMapping("/api/products")
@Slf4j
public class ApiProductController extends ApiBaseAction {
    @Autowired
    private ApiProductService productService;
    @Autowired
    private ApiShopService apiShopService;

    /**
     * 查询商户上架的所有产品
     */
    @ApiOperation(value = " 查询商户上架的所有产品")
    @PostMapping(value = "shopProducts")
    @IgnoreAuth
    public Object shopProducts() {
        JSONObject json = this.getJsonRequest();
        QueryShopProductsInputDto dto = JSONObject.toJavaObject(json, QueryShopProductsInputDto.class);
        //status为0是上架状态
        dto.setStatus(0);
        log.info("上架状态：{}", JSONObject.toJSONString(dto));
        List<ProductVo> result = productService.queryShopCatalogProductList(dto);

        return toResponsSuccess(result);
    }

    /**
     * 查询商户库存的所有产品
     */
    @ApiOperation(value = " 查询商户库存的所有产品")
    @PostMapping(value = "stock")
    public Object stock(@LoginUser UserVo loginUser) {
        //根据userId查询该用户的shopId
        ShopVo vo = apiShopService.queryByUserId(loginUser.getUserId().intValue());

        QueryShopProductsInputDto dto = new QueryShopProductsInputDto();
        dto.setShopId(vo.getShopId());
        List<ProductVo> result = productService.queryShopCatalogProductList(dto);

        return toResponsSuccess(result);
    }

    /**
     * 修改产品信息
     */
    @ApiOperation(value = " 修改产品信息")
    @PostMapping(value = "update")
    public Object update() {
        JSONObject json = this.getJsonRequest();
        ProductVo vo = JSONObject.toJavaObject(json,ProductVo.class);
        productService.update(vo);

        return toResponsSuccess(null);
    }

    /**
     * 查询供应商分类下的产品
     */
    @ApiOperation(value = " 查询供应商分类下的产品")
    @PostMapping(value = "adminProducts")
    public Object adminProducts() {
        List<ProductVo> result = productService.queryAdminCatalogProductList();
        return toResponsSuccess(result);
    }

}